package org.flower.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.flower.dao.AddressDao;
import org.flower.entity.Address;
import org.flower.entity.User;
import org.flower.utils.BaseDao;

public class AddressDaoImpl extends BaseDao implements AddressDao{


    @Override
    public List<Address> getAllAddressByUid(int uid) {
        String sql = "select *,\n" +
                "(select name from province where id=a.`province` ) as sheng,\n" +
                "(select name from province where id=a.`city` ) as shi,\n" +
                "(select name from province where id=a.`area` ) as qu\n" +
                "from address a where userId=?";
        List<Address> aList = new ArrayList<Address>();
        ResultSet rs = super.executeQuery(sql, uid);
        try {
            while (rs.next()){
                Address a = new Address();
                //12个属性
                a.setId(rs.getInt("a.id"));
                a.setUserId(rs.getInt("userId"));
                a.setPhone(rs.getString("phone"));
                a.setAddressContent(rs.getString("addressContent"));
                a.setShouName(rs.getString("shouName"));
                a.setDefault(rs.getBoolean("isDefault"));
                a.setProvince(rs.getInt("province"));
                a.setCity(rs.getInt("city"));
                a.setArea(rs.getInt("area"));
                a.setSheng(rs.getString("sheng"));
                a.setShi(rs.getString("shi"));
                a.setQu(rs.getString("qu"));
                aList.add(a);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                super.closeAll(rs,rs.getStatement(),rs.getStatement().getConnection());
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return aList;
    }

    @Override
    public boolean addAddress(Address a) {
        String sql = "insert into address values(null,?,?,?,?,false,?,?,?)";
        return super.executeUpdate(sql,a.getUserId(),a.getPhone(),a.getAddressContent(),
        a.getShouName(),a.getProvince(),a.getCity(),a.getArea());
    }

    @Override
    public boolean updateAddress(Address a) {
        String sql = "update address set phone=?,addressContent=?,shouName=?,province=?,city=?,`area`=? where id=?";
        return super.executeUpdate(sql,a.getPhone(),a.getAddressContent(),a.getShouName(),
                a.getProvince(),a.getCity(),a.getArea(),a.getId());
    }

    @Override
    public boolean updateSetDefaultById(int id) {
        String sql = "update address set isDefault=true where id=?";
        return super.executeUpdate(sql,id);
    }

    @Override
    public boolean updateUnbindDefault(int id) {
        String sql = "update address set isDefault=false where id=?";
        return super.executeUpdate(sql,id);
    }

    @Override
    public boolean deleteAddressById(int id) {
        String sql = "delete from address where id = ?";
        return super.executeUpdate(sql,id);
    }

    @Override
    public List<Integer> getAllChuDefaultQiTaAddressId(int id, int uid) {
        String sql = "select id from address where isDefault=true and userid=? and id!=?";
        List<Integer> ints = new ArrayList<Integer>();
        ResultSet rs = super.executeQuery(sql, uid, id);
        try {
            while (rs.next()){
                ints.add(rs.getInt(1));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                super.closeAll(rs,rs.getStatement(),rs.getStatement().getConnection());
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return ints;
    }

    @Override
    public int getMaxIdByUid(int uid) {
        int id = 0;
        String sql = "select MAX(id) from address where userId=?";
        ResultSet rs = super.executeQuery(sql, uid);
        try {
            if (rs.next()){
                id = rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                super.closeAll(rs,rs.getStatement(),rs.getStatement().getConnection());
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return id;
    }

    @Override
    public Address getAddressById(int id) {
        String sql = "select *,\n" +
                "(select name from province where id=a.`province` ) as sheng,\n" +
                "(select name from province where id=a.`city` ) as shi,\n" +
                "(select name from province where id=a.`area` ) as qu\n" +
                "from address a where id=?";
        Address a = null;
        ResultSet rs = super.executeQuery(sql, id);
        try {
            if (rs.next()){
                a = new Address();
                //12个属性
                a.setId(rs.getInt("a.id"));
                a.setUserId(rs.getInt("userId"));
                a.setPhone(rs.getString("phone"));
                a.setAddressContent(rs.getString("addressContent"));
                a.setShouName(rs.getString("shouName"));
                a.setDefault(rs.getBoolean("isDefault"));
                a.setProvince(rs.getInt("province"));
                a.setCity(rs.getInt("city"));
                a.setArea(rs.getInt("area"));
                a.setSheng(rs.getString("sheng"));
                a.setShi(rs.getString("shi"));
                a.setQu(rs.getString("qu"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                super.closeAll(rs,rs.getStatement(),rs.getStatement().getConnection());
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return a;
    }

    @Override
    public int getAddressCountByUid(int uid) {
        String sql = "select COUNT(1) from address where userId=?";
        int row = 0;
        ResultSet rs = super.executeQuery(sql, uid);
        try {
            if (rs.next()){
                row = rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                super.closeAll(rs,rs.getStatement(),rs.getStatement().getConnection());
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return row;
    }
}
